package handler

import (
	"github.com/gin-gonic/gin"
	"red-book-pub/model"
	"red-book-pub/pkg"
)

func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	Password := c.PostForm("password")
	if username == "" || Password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空字段",
		})
		return
	}
	var user model.User
	err := model.DB.Where("username", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkg.Md5(Password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return
}
